import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose'
import { Document } from 'mongoose'

@Schema({ timestamps: true })
export class TemplateShare extends Document {
    @Prop({ required: true })
    templateId: string

    @Prop({ required: true })
    shareId: string

    @Prop()
    version?: string

    @Prop()
    password?: string

    @Prop({ default: true })
    isPublic: boolean

    @Prop({ required: true })
    expiresAt: Date

    @Prop({ default: 0 })
    downloadCount: number

    @Prop({ required: true })
    createdBy: string
}

export const TemplateShareSchema = SchemaFactory.createForClass(TemplateShare)

// 定义单个索引，避免重复
TemplateShareSchema.index({ shareId: 1 }, { unique: true })
TemplateShareSchema.index({ expiresAt: 1 })
TemplateShareSchema.index({ templateId: 1 })
TemplateShareSchema.index({ createdBy: 1 })
TemplateShareSchema.index({ isPublic: 1, expiresAt: 1 }) // 用于查询公开且未过期的分享 